<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
//导入element中提供的消息弹窗
import { ElMessage } from 'element-plus'
//导入路由的一个函数
import { useRouter } from 'vue-router'
//导入ts文件定义的接口函数
import { registerService } from "@/api/login"
import { adminLoginService,captchaService } from "@/api/login"
//导入设置token的函数
import { useTokenStore } from '@/stores/token'

const isRegister = ref(false)

//调用后台获取验证码接口
const captcha = async () => {
    let result = await captchaService() as any
    registerData.value.img = result.data.img;
    registerData.value.key = result.data.key;
}
captcha();
//刷新验证码点击事件
const refreshCaptcha = () =>{
    captcha();
}

//用于注册的数据模型
const registerData = ref({
    account: '',
    password: '',
    rePassword: '',
    img: '',
    key: '',
    code:'',
    roleId:2
})
const clearRegisterData = () => {
    registerData.value = {
        account: '',
        password: '',
        rePassword: '',
        img: '',
        key: '',
        code:'',
        roleId:2
    }
}
const clearspecialRegisterData = () => {
    registerData.value.password = '';
    registerData.value.rePassword =  '',
    registerData.value.code = '';
}
//自定义确认密码的校验函数
const rePasswordValid = (rule: any, value: string | null, callback: (arg0: Error) => any) => {
    if (value == null || value === '') {
        return callback(new Error('请再次确认密码'))
    }
    if (registerData.value.password !== value) {
        return callback(new Error('两次输入密码不一致'))
    }
}
//用于注册的表单校验模型
const registerDataRules = ref({
    account: [
        { required: true, message: '请输入用户账号', trigger: 'blur' },
        { min: 4, max: 9, message: '用户名的长度必须为4~9位', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 5, max: 16, message: '密码长度必须为5~16位', trigger: 'blur' }
    ],
    rePassword: [
        { validator: rePasswordValid, trigger: 'blur' }
    ],
    code: [
        { required: true, message: '请输入验证码', trigger: 'blur' }
    ]
})



//调用后台接口,完成注册
const register = async () => {
    let result = await registerService(registerData.value) as any
    ElMessage.success(result.msg ? result.msg : '注册成功')
}
//调用后台接口，完成登录
const router = useRouter();
const tokenStore = useTokenStore();
const login = async () => {
    let result = await adminLoginService(registerData.value) as any
    ElMessage.success(result.msg ? result.msg : '登录成功')
    //设置token值
    tokenStore.setToken(result.data)
    if(registerData.value.roleId == 1){
        router.push('/admin/home')
    }else if(registerData.value.roleId == 2){
        router.push('/user/home')
    }
}

</script>
<template>
    <div class="login-bg">
        <el-row class="login-page">
        <el-col :span="6" :offset="3" class="form">
            <!-- 注册表单 -->
            <el-form ref="form" size="large" autocomplete="off" v-if="isRegister" :model="registerData"
                :rules="registerDataRules">
                <el-form-item>
                    <h1>注册</h1>
                </el-form-item>
                <el-form-item prop="account">
                    <el-input :prefix-icon="User" placeholder="请输入用户账号" v-model="registerData.account"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码"
                        v-model="registerData.password"></el-input>
                </el-form-item>
                <el-form-item prop="rePassword">
                    <el-input :prefix-icon="Lock" type="password" placeholder="请输入再次密码"
                        v-model="registerData.rePassword"></el-input>
                </el-form-item>
                <!-- 注册按钮 -->
                <el-form-item>
                    <el-button class="button" type="primary" auto-insert-space @click="register();refreshCaptcha();clearspecialRegisterData()">
                        注册
                    </el-button>
                </el-form-item>
                <el-form-item class="flex">
                    <el-link type="info" :underline="false" @click="isRegister = false; clearRegisterData();refreshCaptcha()">
                        ← 返回
                    </el-link>
                </el-form-item>
            </el-form>
            <!-- 登录表单 -->
            <el-form ref="form" size="large" autocomplete="off" v-else :model="registerData" :rules="registerDataRules">
                <el-form-item>
                    <h1>登录</h1>
                </el-form-item>
                <el-form-item prop="account">
                    <el-input :prefix-icon="User" placeholder="请输入用户账号" v-model="registerData.account"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码"
                        v-model="registerData.password"></el-input>
                </el-form-item>
                <el-form-item prop="code">
                    <div style="display: flex; justify-content: center">
                        <el-input v-model="registerData.code" :prefix-icon="Lock" placeholder="请输入验证码"></el-input>
                        <img :src="registerData.img" @click="refreshCaptcha()" width="180px" height="40px"/>
                    </div>
                </el-form-item>
                <el-form-item prop="roleId" >
                    <el-radio-group v-model="registerData.roleId">
                        <el-radio value="1" border>管理员</el-radio>
                        <el-radio value="2" border>用户</el-radio>
                    </el-radio-group>
                </el-form-item>
                <!-- 登录按钮 -->
                <el-form-item>
                    <el-button class="button" type="primary" auto-insert-space @click="login();refreshCaptcha();clearspecialRegisterData()">登录</el-button>
                </el-form-item>
                <el-form-item class="flex">
                    <el-link type="info" :underline="false" @click="isRegister = true; clearRegisterData()">
                        注册 →
                    </el-link>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row> 
</div>
    
</template>

<style lang="scss" scoped>
    .login-bg{
        width: 100%;
        min-height: 100vh;
        background-size: 100% 100%;
        background-image: url("../assets/background.jpg");
        background-repeat: no-repeat;
    }
    .form {
        width: 500px;
        margin: 200px auto;
        background-color: white;
        padding: 30px;
        border-radius: 20px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        user-select: none;
        .title {
            margin: 0 auto;
        }
        .button {
            width: 100%;
        }
        .flex {
            width: 100%;
            display: flex;
            justify-content: space-between;
        }
    }
</style>